﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class CategoryesForm : Form
    {
        string CatName;
        int MaxIndex = 0;



        public CategoryesForm()
        {
            InitializeComponent();
        }

        public CategoryesForm(string CatName)
        {
            InitializeComponent();
            this.CatName = CatName;
        }

        private void CategoryesForm_Load(object sender, EventArgs e)
        {

            //MaxIndex = DBClass.Categoryes.Max(m => m.CatIndex);


            cbCategory.Items.AddRange(UtilClass.GetAllCategory());
            tbRusName.Enabled = tbUzbName.Enabled = addButton.Enabled = false;
            if (!String.IsNullOrEmpty(CatName))
            {
                int ind = -1;
                for (int i = 0; i < InformationClass.Categories.Length; i++)
                {
                    if (InformationClass.Categories[i] == CatName)
                    {
                        ind = i;
                        break;
                    }
                }

                if (InformationClass.Lang)
                {
                    cbCategory.SelectedIndex = cbCategory.Items.IndexOf(InformationClass.CategoriesRus[ind]);
                }
                else
                {
                    cbCategory.SelectedIndex = cbCategory.Items.IndexOf(InformationClass.CategoriesUzb[ind]);
                }
            }
        }

        private void cbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            CatName = InformationClass.Categories[cbCategory.SelectedIndex];
            try
            {
                MaxIndex = DBClass.Categoryes.Where(wh => wh.CatName == this.CatName).Max(m => m.CatIndex);
            }
            catch (InvalidOperationException)
            {
                MaxIndex = 0;
            }
            FillGrid();
        }

        private void FillGrid()
        {
            categoryGrid.DataSource = DBClass.Categoryes
                .Where(wh => wh.CatName == CatName).ToList();
            categoryGrid.Columns["CatIndex"].Visible = categoryGrid.Columns["CatName"].Visible = false;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

            DBClass.RESPContext.SubmitChanges();

            this.Close();
        }

        private void addNewStripButton_Click(object sender, EventArgs e)
        {
            tbRusName.Enabled = tbUzbName.Enabled = addButton.Enabled = true;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbUzbName.Text) || string.IsNullOrEmpty(tbRusName.Text))
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Обязательно заполнение обоих полей", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show("Сатрларнинг иккисини тўлдирилиши шарт", "Хато!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            tb_category newCat = new tb_category
            {
                CatIndex = ++MaxIndex,
                CatName = this.CatName,
                NameRus = tbRusName.Text,
                NameUz = tbUzbName.Text
            };

            DBClass.Categoryes.InsertOnSubmit(newCat);
            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }


            tbRusName.Enabled = tbUzbName.Enabled = addButton.Enabled = false;
            tbRusName.Text = tbUzbName.Text = string.Empty;
            FillGrid();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
